<?php

class JamView {

	protected $_vars;
	protected $_name;
	protected $_viewContent;

	public function  __construct($name) {
		$this->_name = $name;
		$this->_template = JAM_TEMPLATE;
		$this->_vars = new stdClass();
	}

	public function setVar($varName, $varValue){
		$this->_vars->$varName = $varValue;
	}

	public function getVar($varName, $default = null){
		if(isset($this->_vars->$varName))
			return $this->_vars->$varName;
		else
			return $default;
	}

	public function getPiece($name, $getGlobalFile = false){
		$viewSpecificFile = JB_FE_TEMPLATES . DS . $this->_template . DS . 'scripts' . DS . $this->_name . DS . JString::strtolower($name) . '.php';
		$globalFile = JB_FE_TEMPLATES . DS . $this->_template . DS . 'scripts' . DS . JString::strtolower($name) . '.php';

		if(file_exists($viewSpecificFile) && $getGlobalFile == false){
			ob_start();
			include($viewSpecificFile);
			$content = ob_get_clean();
		} else if(file_exists($globalFile)){
			ob_start();
			include($globalFile);
			$content = ob_get_clean();
		} else {
			$content = '';
		}

		return $content;
	}

	public function display($action = 'index'){
		$contentFile = JB_FE_TEMPLATES . DS . $this->_template . DS . 'scripts' . DS . $this->_name . DS . JString::strtolower($action) . '.php';

		if(file_exists($contentFile)){
			ob_start();
			include($contentFile);
			$viewContent = ob_get_clean();
		} else {
			$viewContent = '';
		}

		echo $viewContent;
	}
}

?>